package com.example.userp.app.myapplication.AlphabeticIndex;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.example.userp.app.myapplication.R;


/**
 * Describe: 字母索引
 * Created by ysy on 2018/9/12.
 */
public class AlphabeticIndexView extends View {

    private String [] mAlphabetics= {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private int mTextColor;
    private float mTextSize;

    AlphabeticsClick mAlphabeticsClick;//点击事件
    TextView mTextView;//提示view
    int mLastpos=-1;//最后展示的索引 的pos

    public AlphabeticIndexView(Context context) {
        super(context);
    }

    public AlphabeticIndexView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AlphabeticIndex);
        mTextColor = typedArray.getColor(R.styleable.AlphabeticIndex_textcolor, Color.GRAY);
        mTextSize = typedArray.getFloat(R.styleable.AlphabeticIndex_textsize, getWidth()/3);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint=new Paint();
        paint.setAntiAlias(true);
        paint.setColor(mTextColor);
        int width=getWidth();
        int height = getHeight();
        int oneAlphabeticheight= height/mAlphabetics.length;
        if (mTextSize>oneAlphabeticheight){
            mTextSize= oneAlphabeticheight*0.9f;
        }
        paint.setTextSize(mTextSize);

        for (int i=0;i>mAlphabetics.length;i++){
            float alphaetcs_x=width/2-paint.measureText(mAlphabetics[i])/2;
            float alphaetcs_y=oneAlphabeticheight*i+oneAlphabeticheight;
            canvas.drawText(mAlphabetics[i],alphaetcs_x,alphaetcs_y,paint);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        float y = event.getY();
        int pos= (int) (y/getHeight()*mAlphabetics.length);
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                if (pos >= 0&&pos<mAlphabetics.length&& pos!=mLastpos){
                    mLastpos=pos;
                    if (mAlphabeticsClick!=null){
                        mAlphabeticsClick.onAlphabeticsClick(mAlphabetics[pos],pos);
                    }
                    if (mTextView!=null){
                        mTextView.setText(mAlphabetics[pos]);
                        mTextView.setVisibility(VISIBLE);
                    }
                    invalidate();
                }

                break;
            case MotionEvent.ACTION_DOWN:
                if (pos >= 0&&pos<mAlphabetics.length&& pos!=mLastpos) {
                    mLastpos=pos;
                    if (mAlphabeticsClick != null) {
                        mAlphabeticsClick.onAlphabeticsClick(mAlphabetics[pos], pos);
                    }
                    if (mTextView != null) {
                        mTextView.setText(mAlphabetics[pos]);
                        mTextView.setVisibility(VISIBLE);
                    }
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                mLastpos=-1;
                if (mTextView!=null){
                    mTextView.setVisibility(GONE);
                }
                invalidate();
                break;
        }

        return true;
    }

    /**
     * 提示的View
     * @param view
     */
    public void setPromptTextView(TextView view){
        this.mTextView=view;
        if (mTextView!=null){
            mTextView.setVisibility(GONE);
        }
    }

    /**
     * 点击事件
     * @param alphabeticsClick
     */
    public void setonAlphabeticsClick(AlphabeticsClick alphabeticsClick){
        this.mAlphabeticsClick=alphabeticsClick;
    }
}
